package com.lin.manager.oss.config;

import com.lin.manager.oss.config.properties.OssProperties;
import com.lin.manager.oss.service.impl.OssServiceImpl;
import com.lin.manager.oss.storage.StorageEngine;
import com.lin.manager.oss.storage.engine.aliyun.AliyunStorageClientFactory;
import com.lin.manager.oss.storage.engine.aliyun.AliyunStorageEngine;
import com.lin.manager.oss.storage.engine.local.LocalStorageEngine;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author linjiang
 * @date 2024/7/26
 * @fn
 */
@Configuration
@ConditionalOnBean(OssProperties.class)
public class OssConfig {
    @Resource
    OssProperties ossProperties;

    @Bean
    @ConditionalOnProperty(prefix = "file.storage", name = {"platform"}, havingValue = "aliyun")
    public AliyunStorageEngine aliyunStorageEngine() {
        AliyunStorageClientFactory factory = new AliyunStorageClientFactory(ossProperties.getAliyun());
        return new AliyunStorageEngine(ossProperties.getAliyun().getBucket(), factory);
    }

    @Bean
    @ConditionalOnProperty(prefix = "file.storage", name = {"platform"}, havingValue = "local")
    public LocalStorageEngine localStorageEngine(OssProperties ossProperties) {
        return new LocalStorageEngine(ossProperties.getLocal().getBucket());
    }

    @Bean
    public OssServiceImpl fileService(StorageEngine storageEngine) {
        return new OssServiceImpl(storageEngine);
    }

}
